package com.jxc.action.spgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VBrand;
import com.jxc.model.vo.VDiscount;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.DiscountServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/spgl")
@Action(value = "discountAction", results = { @Result(name = "discount", location = "/discount/discount.jsp"),
		@Result(name = "discountAdd", location = "/discount/discountAdd.jsp"),
		@Result(name = "discountEdit", location = "/discount/discountEdit.jsp") })
public class DiscountAction extends BaseAction implements ModelDriven<VDiscount> {
	private final static Logger logger = Logger.getLogger(DiscountAction.class);

	private VDiscount model = new VDiscount();

	@Autowired
	private DiscountServiceI discountService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	public String discount() {
		getSubAuths("SPGL_ZKGL");
		return "discount";
	}

	public String discountAdd() {
		return "discountAdd";
	}

	public String discountEdit() {
		return "discountEdit";
	}
	
	public void noAuth_Combobox(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest()
				.getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
//		如果当前用户仓库为空,说明是经销商或代理商用户,如果不为空,那么他可以看到他的上级的折扣
		if(vuser.getReid()==null){
			model.setUid(sessionInfo.getUserId());
			this.writeJson(discountService.combobox(model));
		}else{
			model.setUid(vuser.getUpid());
			this.writeJson(discountService.combobox(model));
		}

	}
	
	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
		.getRequest().getSession().getAttribute(
				ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
//		如果当前用户仓库为空,说明是经销商或代理商用户,如果不为空,那么他可以看到他的上级的折扣
		if(vuser.getReid()==null){
			model.setUid(sessionInfo.getUserId());
			this.writeJson(discountService.datagrid(model));
		}else{
			model.setUid(vuser.getUpid());
			this.writeJson(discountService.datagrid(model));
		}

	}

	/**
	 * 添加品牌
	 * 
	 */
	public void add() {
		Json j = new Json();
		try {SessionInfo sessionInfo = (SessionInfo) ServletActionContext
			.getRequest().getSession().getAttribute(
					ResourceUtil.getSessionInfoName());
			model.setUid(sessionInfo.getUserId());
			discountService.save(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败，折扣已存在！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	/**
	 * 修改品牌
	 * 
	 */
	public void edit() {
		Json j = new Json();
		try {
			discountService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败，折扣名称已存在！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除品牌
	 * 
	 */
	public void delete() {
		Json j = new Json();
		try {
			discountService.delete(model.getIds());
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败，有会员或品牌使用此折扣！");
		}
		super.writeJson(j);
	}

	@Override
	public VDiscount getModel() {
		return model;
	}

}
